var music = {
	getlocalStorage: function() {
		return localStorage;
	},
	spanHtml: function() {
		document.querySelector('.musicname').innerText = music.getlocalStorage().getItem('carname');
	},
	getmusic: function() {
		ajax({
			type: 'get',
			dataType: 'json',
			url: "http://baojun.customer.agokara.com/v1/music/" + music.getlocalStorage().getItem('carcode'),
			success: function(data) {
				music.getdata(data);
			}
		})
	},
	getdata: function(data) {
		var str = '<ul class="list_all">';
		for(var i = 0; i < data.length; i++) {
			str += "<li class='list'><span class></span>" +
				"<div class='music_name' data-hash="+data[i].music_hash+">" +
				"<h4>" + data[i].music_name + "</h4>" +
				"<p>" + data[i].music_singer + "</p>" +
				"</div>" +
				"<span class='paly_music'></span>" +
				"</li>";
		}
		str += '</ul>';
		document.querySelector('.music_list').innerHTML = str;
		music.isscroll();
		for(var i=0;i<document.querySelectorAll('.music_name').length;i++){
//			music.listclick(document.querySelectorAll('.music_name')[i]);
			music.listclick($('.music_name').eq(i));
		}
		for(var i=0;i<document.querySelectorAll('.paly_music').length;i++){
//			music.clickplay(document.querySelectorAll('.paly_music')[i]);
			music.clickplay($('.paly_music').eq(i))
		}
		
	},
	evclick: function(ele) {
		$("."+ele.className).on("touchend",function() {
			var arr = [];
			for(var i=0;i<document.querySelectorAll('.dian').length;i++){
				var str = document.querySelectorAll('.dian')[i].parentNode.children[1];
				var objdian = {};
				objdian.hash = str.getAttribute('data-hash');
				objdian.name = str.children[0].innerText;
				objdian.singer = str.children[1].innerText;
				arr.push(objdian);
			}
			localStorage.setItem('music_arr',JSON.stringify(arr));	
			if(arr.length!=0&&arr.length==3){
				window.location = 'album.html';
			}else{
				alert("需要选择三首歌哦...")
			}			
		})
	},
	listclick: function(ele){
		ele.bind("tap",function() {
			if(this.parentNode.children[0].className == 'dian'){
				this.parentNode.children[0].className = '';
			}else{
				this.parentNode.children[0].className = 'dian';
			}
			
			if(document.querySelectorAll('.dian').length>3){
				this.parentNode.children[0].className = '';
			}
			
			
		})
	},
	//点击播放
	clickplay: function(ele){
		ele.bind("touchend",function(){
			var audio = document.querySelector('audio');
			audio.load();
			if(this.className == 'paly_music'){
				for(var i= 0;i<this.parentNode.parentNode.children.length;i++){
					this.parentNode.parentNode.children[i].children[2].className = "paly_music";
				}
				this.className = 'paly_music2';
				$.ajax({
					url: "http://wuyang.customer.agokara.com/kugou/getmusicurl.do",
					data: {"hash": this.parentNode.children[1].getAttribute('data-hash')},
					dataType: 'jsonp',
					async:false,
					type:"get",
					success: function(data) {
						console.log(data.url);
						audio.src = data.url;
						if(audio.paused){
				            audio.play();
				            
				        }else{
				            audio.pause();
				        }
					},
				    error:function(){
				        console.log('shibai');
				    }
				});
			}else{
				this.className = 'paly_music';
				audio.pause();
			}
		})
	},
	isscroll: function() {
		setTimeout(function() {
			new IScroll('.music_list', {
				scrollY: true,
				tap: true
			});
		}, 150);
	}
}
music.spanHtml();
music.getmusic();
music.evclick(document.querySelector('.music_album'));